package etf.db40;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.query.Query;

import etf.library.*;

import java.io.File;
import java.util.List;

public class Db4oUtil {
	
	static String lokacijaFajla = System.getProperty("user.home");
	EmbeddedConfiguration config;
	ObjectContainer db;
	
	public void createDatabase(String db4oFileName){
		String fileName = lokacijaFajla +  db4oFileName;
		try {
//			deleteDBFile(fileName);
			config = Db4oEmbedded.newConfiguration();
//			config.common().messageLevel(1);
			createIndexedField(IndexedAddress.class, "houseNumber");
			createIndexedField(IndexedAddress.class, "street");
			config.common().objectClass("Author").cascadeOnDelete(true);
			config.common().objectClass(Address.class).objectField("zipPostCode").indexed(true);
			config.common().objectClass(Employee.class).objectField("address").indexed(true);
			config.common().objectClass(Author.class).objectField("books").indexed(true);
			
			config.common().objectClass(Book.class).objectField("year").indexed(true);			
			config.common().objectClass(Member.class).objectField("rentedBooks").indexed(true);
			config.common().objectClass("Author").cascadeOnDelete(true);
			
			config.file().blobPath("imagesDb4o");
			db = Db4oEmbedded.openFile(config, fileName);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	public void getIndexedConfig(){
		config = Db4oEmbedded.newConfiguration();
//		config.common().messageLevel(1);
		createIndexedField(IndexedAddress.class, "houseNumber");
		createIndexedField(IndexedAddress.class, "street");
	}
	
	public void reopenDatabase(String db4oFileName){
		String fileName = lokacijaFajla +  db4oFileName;
		db.close();
		db = Db4oEmbedded.openFile(config, fileName);
	}
	
	public boolean deleteDBFile(String fileName){
		return new File(fileName).delete();
	}
	
	public static void listResult(List<?> result){
    	System.out.println(result.size());
        for (Object o : result) {
            System.out.println(o);
        }
    }
    
	protected void createIndexedField(Class classType, String fieldName) {
		config.common().objectClass(classType).objectField(fieldName).indexed(true);
	}
	
	protected void dropIndexedField(Class classType, String fieldName){
		config.common().objectClass(classType).objectField(fieldName).indexed(false);
	}
	
    public void listRefreshedResult(ObjectContainer container,ObjectSet result,int depth) {
        System.out.println(result.size());
        while(result.hasNext()) {
            Object obj = result.next();
            container.ext().refresh(obj, depth);
            System.out.println(obj);
        }
    }
    
    public void retrieveAll(){
        ObjectSet result=db.queryByExample(new Object());
        listResult(result);
    }
    
    public void deleteEntireDb() {
        ObjectSet result=db.queryByExample(new Object());
        while(result.hasNext()) {
            db.delete(result.next());
        }
    }
    
    public void deleteSpecificSODA(Object o){
    	Query allAddresses = db.query();
 		allAddresses.constrain( Address.class );
 		ObjectSet resultSet = allAddresses.execute();
 		for (int i = 0; i < resultSet.size(); i++) {
 			db.delete(resultSet.next());
		}
    }
    
    
    public void deleteSpecific(Object o){
		ObjectSet result = db.queryByExample(o);
		while (result.hasNext()) {
			db.delete(result.next());
		}
    }
	public void closeDatabase() {
		db.close();
	}
	
    
}